package cn.dnuihyy406.dnuifood_16110100406.controller.collectfragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.CollectBean;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.CollectModel;

public class CFoodFragment extends Fragment implements OnRetrofitListener<List<CollectBean>> {
    private RecyclerView recyclerView;   //先定义一个recycleview
    private CFoodAdapter cFoodAdapter;
    String user_id;
    SharedPreferences sharedPreferences;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.c_food_fragment,container,false);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
        initData();
    }

    @Override
    public void onSuccess(List<CollectBean> object, int flag) {
        cFoodAdapter=new CFoodAdapter(getActivity(),object);//实例化一个adapter对象
        recyclerView.setAdapter(cFoodAdapter);//把适配器的加载到recycleview显示出来
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
    }

    void initViews(){
        recyclerView=getActivity().findViewById(R.id.f2);//找到并绑定控件
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    void initData(){
        sharedPreferences = getActivity().getSharedPreferences("hyy", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("userid","");
        getcollectfood();
    }

    private void getcollectfood(){
        CollectModel collectModel=new CollectModel();
        collectModel.getAllUserCollection(user_id,"1",this);// 向服务器发送请求，成功就调用onsuccess函数
    }
}
